﻿Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Windows.Input
Imports Microsoft.Practices.Prism.Commands

Public Class ViewModelNewPMTWizard
    Implements INotifyPropertyChanged

#Region " Constructor "

    Public Sub New()
        'Création d'un nouveau PMT
        Dim ParametresGeneraux As New PMTParametresGeneraux()
        Dim TablesFlux As New PMTTablesflux()
        _objPMT = New PMT(ParametresGeneraux, TablesFlux)
        CurrentPage = Pages(0)
    End Sub

#End Region

#Region " Declarations "
    Private _cmdCancelCommand As ICommand
    Private _cmdMoveNextCommand As ICommand
    Private _cmdMovePreviousCommand As ICommand
    Private _objPMT As IPMT
    Private _objCurrentPage As ViewModelNewPMTWizardPageBase
    Private _objPages As ReadOnlyCollection(Of ViewModelNewPMTWizardPageBase)
#End Region

#Region " Properties "

    Public ReadOnly Property PMT() As IPMT
        Get
            Return _objPMT
        End Get
    End Property

    Public Property CurrentPage() As ViewModelNewPMTWizardPageBase
        Get
            Return _objCurrentPage
        End Get

        Private Set(ByVal value As ViewModelNewPMTWizardPageBase)

            If _objCurrentPage IsNot Nothing Then
                _objCurrentPage.IsCurrentPage = False
            End If

            _objCurrentPage = value

            If _objCurrentPage IsNot Nothing Then
                _objCurrentPage.IsCurrentPage = True
            End If

            OnPropertyChanged("CurrentPage")
            OnPropertyChanged("IsOnLastPage")
        End Set
    End Property

    Private ReadOnly Property CurrentPageIndex() As Integer
        Get

            If Me.CurrentPage Is Nothing Then
                Debug.Fail("why is current page null?")
                Return -1

            Else
                Return Me.Pages.IndexOf(Me.CurrentPage)
            End If

        End Get
    End Property

    Public ReadOnly Property IsOnLastPage() As Boolean
        Get
            Return Me.CurrentPageIndex = Me.Pages.Count - 1
        End Get
    End Property

    Public ReadOnly Property IsOnFirstPage As Boolean
        Get
            Return Me.CurrentPageIndex = 0
        End Get
    End Property

    Public ReadOnly Property Pages() As ReadOnlyCollection(Of ViewModelNewPMTWizardPageBase)
        Get

            If _objPages Is Nothing Then
                Me.CreatePages()
            End If

            Return _objPages
        End Get
    End Property

#End Region

#Region " Command Properties "

    Public ReadOnly Property CancelCommand() As ICommand
        Get

            If _cmdCancelCommand Is Nothing Then
                _cmdCancelCommand = New DelegateCommand(Of Object)(AddressOf ExecuteCancel)
            End If

            Return _cmdCancelCommand
        End Get
    End Property

    Public ReadOnly Property MoveNextCommand() As ICommand
        Get

            If _cmdMoveNextCommand Is Nothing Then
                _cmdMoveNextCommand = New DelegateCommand(Of Object)(AddressOf ExecuteMoveNext)
            End If

            Return _cmdMoveNextCommand
        End Get
    End Property

    Public ReadOnly Property MovePreviousCommand() As ICommand
        Get

            If _cmdMovePreviousCommand Is Nothing Then
                _cmdMovePreviousCommand = New DelegateCommand(Of Object)(AddressOf ExecuteMovePrevious)
            End If

            Return _cmdMovePreviousCommand
        End Get
    End Property

#End Region

#Region " Methods "

    Private Sub CreatePages()

        Dim objViewModelNewPMTWizardParamGen As New ViewModelNewPMTWizardPageParamGen(PMT)
        Dim objViewModelNewPMTWizardTblesFlux As New ViewModelNewPMTWizardPageTblesFlux(PMT)
        Dim obj As New List(Of ViewModelNewPMTWizardPageBase)
        With obj
            '.Add(New WelcomePageViewModel)
            .Add(objViewModelNewPMTWizardParamGen)
            .Add(objViewModelNewPMTWizardTblesFlux)
            '.Add(New CoffeeSummaryPageViewModel( _
            '     objCoffeeTypeSizePageViewModel.AvailableBeanTypes, _
            '     objCoffeeTypeSizePageViewModel.AvailableDrinkSizes, _
            '     objCoffeeExtrasPageViewModel.AvailableFlavorings, _
            '     objCoffeeExtrasPageViewModel.AvailableTemperatures))
        End With
        _objPages = New ReadOnlyCollection(Of ViewModelNewPMTWizardPageBase)(obj)
    End Sub

    Protected Sub OnPropertyChanged(ByVal strPropertyName As String)

        Dim handler As PropertyChangedEventHandler = Me.PropertyChangedEvent

        If handler IsNot Nothing Then
            handler.Invoke(Me, New PropertyChangedEventArgs(strPropertyName))
        End If

    End Sub

#End Region

#Region "Event"
    Public Event PropertyChanged As PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    Public Event RequestClose As EventHandler
#End Region

#Region " Command Methods "

    Private Function CanExecuteMoveNext(ByVal param As Object) As Boolean

        If Me.CurrentPage IsNot Nothing Then
            Return Me.CurrentPage.IsValid

        Else
            Return False
        End If

    End Function
    Private Sub ExecuteMoveNext(ByVal parm As Object)

        If CanExecuteMoveNext(Nothing) Then

            Dim intIndex As Integer = Me.CurrentPageIndex

            If intIndex < Me.Pages.Count - 1 Then
                Me.CurrentPage = Me.Pages(intIndex + 1)
                OnPropertyChanged("IsOnLastPage")
                OnPropertyChanged("IsOnFirstPage")
            Else
                OnRequestClose()
            End If

        End If

    End Sub

    Private Function CanExecuteMovePrevious(ByVal param As Object) As Boolean
        Return Me.CurrentPageIndex > 0
    End Function
    Private Sub ExecuteMovePrevious(ByVal param As Object)

        If CanExecuteMovePrevious(Nothing) Then

            Dim intIndex As Integer = Me.CurrentPageIndex

            If intIndex > 0 Then
                Me.CurrentPage = Me.Pages(intIndex - 1)
                OnPropertyChanged("IsOnLastPage")
                OnPropertyChanged("IsOnFirstPage")
            End If

        End If

    End Sub

    Private Sub ExecuteCancel(ByVal param As Object)
        _objPMT = Nothing
        OnRequestClose()
    End Sub

    Private Sub OnRequestClose()

        Dim handler As EventHandler = Me.RequestCloseEvent

        If handler IsNot Nothing Then
            handler.Invoke(Me, EventArgs.Empty)
        End If

    End Sub

#End Region

End Class